﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class XetCongBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();

        public List<XETCONG> LayTatCa()
        {
            return DB.XETCONGs.ToList();
        }

        public void Them(XETCONG n)
        {
            DB.XETCONGs.InsertOnSubmit(n);
            DB.SubmitChanges();
        }

        public void Sua(XETCONG n)
        {
            XETCONG o = DB.XETCONGs.SingleOrDefault(a => a.MaXetCong == n.MaXetCong);
            if (n.LyDo == null) { }
            else
            {
                o.LyDo = n.LyDo;
            }

            if (n.Ngay == null) { }
            else
            {
                o.Ngay = n.Ngay;
            }

            if (n.MaNV == null) { }
            else
            {
                o.MaNV = n.MaNV;
            }

            if (n.SoCong == null) { }
            else
            {
                o.SoCong = n.SoCong;
            }

            if (n.TGTangCa == null) { }
            else
            {
                o.TGTangCa = n.TGTangCa;
            }

            if (n.HeSoTC == null) { }
            else
            {
                o.HeSoTC = n.HeSoTC;
            }

            if (n.TrangThaiDuyet == null) { }
            else
            {
                o.TrangThaiDuyet = n.TrangThaiDuyet;
            }

            if (n.ChuThich == null) { }
            else
            {
                o.ChuThich = n.ChuThich;
            }

            if (n.TrangThaiDoc == null)
            { }
            else
            {
                o.TrangThaiDoc = n.TrangThaiDoc;
            }
            DB.SubmitChanges();
        }

        public bool KiemTraTonTai(int manv, DateTime ngay)
        {
            if (DB.XETCONGs.Any(a => a.MaNV == manv && a.Ngay == ngay))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public List<XETCONG2> LayDSXetCong(int _manv, int _mapb, int _ttd, int _pv)
        {
            // Show all
            if (_manv == 0 && _pv == 0)
            {
                if (_ttd == 3)
                {
                    List<XETCONG2> ls = (from a in DB.XETCONGs
                                         orderby a.MaXetCong descending
                                         where a.NHANVIEN.MaPB == _mapb
                                         select new XETCONG2()
                                         {
                                             Ma = a.MaXetCong,
                                             Ngay = a.Ngay.Value,
                                             MaNV = (int)a.MaNV,
                                             HoTen = a.NHANVIEN.HoTen,
                                             TrangThaiDuyet = a.TrangThaiDuyet ?? 0,
                                             SoCong = a.SoCong ?? 0,
                                             MaLoaiCong = a.MaLoaiCong,
                                             GioTangCa = a.TGTangCa ?? 0,
                                             HeSo = a.HeSoTC ?? 0
                                         }).ToList();
                    return ls;
                }
                else if (_ttd < 3 && _ttd >= 0)
                {
                    List<XETCONG2> ls = (from a in DB.XETCONGs
                                         orderby a.MaXetCong descending
                                         where a.TrangThaiDuyet == _ttd && a.NHANVIEN.MaPB == _mapb
                                         select new XETCONG2()
                                         {
                                             Ma = a.MaXetCong,
                                             Ngay = a.Ngay.Value,
                                             MaNV = (int)a.MaNV,
                                             HoTen = a.NHANVIEN.HoTen,
                                             TrangThaiDuyet = a.TrangThaiDuyet ?? 0,
                                             SoCong = a.SoCong ?? 0,
                                             MaLoaiCong = a.MaLoaiCong,
                                             GioTangCa = a.TGTangCa ?? 0,
                                             HeSo = a.HeSoTC ?? 0
                                         }).ToList();
                    return ls;
                }
                else return null;

            }
            //Show one person
            else if (_manv != 0 && _pv != 0)
            {
                if (_ttd == 3)
                {
                    List<XETCONG2> ls = (from a in DB.XETCONGs
                                         orderby a.MaXetCong descending
                                         where a.MaNV == _manv
                                         select new XETCONG2()
                                         {
                                             Ma = a.MaXetCong,
                                             Ngay = a.Ngay.Value,
                                             MaNV = (int)a.MaNV,
                                             HoTen = a.NHANVIEN.HoTen,
                                             TrangThaiDuyet = a.TrangThaiDuyet ?? 0,
                                             SoCong = a.SoCong ?? 0,
                                             MaLoaiCong = a.MaLoaiCong,
                                             GioTangCa = a.TGTangCa ?? 0,
                                             HeSo = a.HeSoTC ?? 0
                                         }).ToList();
                    return ls;
                }
                else
                {
                    List<XETCONG2> ls = (from a in DB.XETCONGs
                                         orderby a.MaXetCong descending
                                         where a.TrangThaiDuyet == _ttd && a.MaNV == _manv
                                         select new XETCONG2()
                                         {
                                             Ma = a.MaXetCong,
                                             Ngay = a.Ngay.Value,
                                             MaNV = (int)a.MaNV,
                                             HoTen = a.NHANVIEN.HoTen,
                                             TrangThaiDuyet = a.TrangThaiDuyet ?? 0,
                                             SoCong = a.SoCong ?? 0,
                                             MaLoaiCong = a.MaLoaiCong,
                                             GioTangCa = a.TGTangCa ?? 0,
                                             HeSo = a.HeSoTC ?? 0
                                         }).ToList();
                    return ls;
                }


            }
            else
            {
                return null;
            }

        }

        
        public List<XETCONG2> LayDSXetCongNV(int _manv, int _ttd)
        {
            if (_ttd == 0 || _ttd == 2)//chờ duyệt
            {
                List<XETCONG2> _ls = (from a in DB.XETCONGs
                                      orderby a.MaXetCong descending
                                      where a.MaNV == _manv && a.TrangThaiDuyet == _ttd
                                      select new XETCONG2()
                                      {
                                          Ma = a.MaXetCong,
                                          Ngay = a.Ngay.Value,
                                          MaNV = (int)a.MaNV,
                                          HoTen = a.NHANVIEN.HoTen,
                                          TrangThaiDuyet = a.TrangThaiDuyet ?? 0,
                                          SoCong = a.SoCong ?? 0,
                                          MaLoaiCong = a.MaLoaiCong,
                                          GioTangCa = a.TGTangCa ?? 0,
                                          HeSo = a.HeSoTC ?? 0,
                                          TrangThaiDoc = a.TrangThaiDoc ?? 1
                                      }).ToList();
                return _ls;
            }
            else
            {
                List<XETCONG2> _ls = (from a in DB.XETCONGs
                                      orderby a.MaXetCong descending
                                      where a.MaNV == _manv && a.TrangThaiDuyet == _ttd
                                      select new XETCONG2()
                                      {
                                          Ma = a.MaXetCong,
                                          Ngay = a.Ngay.Value,
                                          MaNV = (int)a.MaNV,
                                          HoTen = a.NHANVIEN.HoTen,
                                          TrangThaiDuyet = a.TrangThaiDuyet ?? 0,
                                          SoCong = a.SoCong ?? 0,
                                          MaLoaiCong = a.MaLoaiCong,
                                          GioTangCa = a.TGTangCa ?? 0,
                                          HeSo = a.HeSoTC ?? 0,
                                          TrangThaiDoc = a.TrangThaiDoc ?? 1
                                      }).ToList();
                return _ls;
            }            
        }


        public XETCONG LayXetCong(int ma)
        {
            return DB.XETCONGs.SingleOrDefault(a => a.MaXetCong == ma);
        }

        public void Xoa(int ma)
        {
            XETCONG n = DB.XETCONGs.SingleOrDefault(a => a.MaXetCong == ma);
            if (n != null)
            {
                DB.XETCONGs.DeleteOnSubmit(n);
                DB.SubmitChanges();
            }
        }

        public int DemThongBao(int _maNV, int _maPB, int _mattd)
        {
            if (_maPB == 0)
            {
                List<XETCONG> ls = (from a in DB.XETCONGs
                                    where a.MaNV == _maNV && a.TrangThaiDuyet == _mattd && a.TrangThaiDoc == 0
                                    select a).ToList();
                if (ls != null)
                {
                    return ls.Count;
                }
                else
                    return 0;
            }
            else
            {
                List<XETCONG> ls = (from a in DB.XETCONGs
                                    where a.NHANVIEN.MaPB == _maPB && a.TrangThaiDuyet == _mattd
                                    select a).ToList();
                if (ls != null)
                {
                    return ls.Count;
                }
                else
                    return 0;
            }
        }
    }
}
